[Any key to pause, ^C to abort] Welcome the first installment of The Hack C Tutorial. The format of these tutorials will based on the Q/A format, with an imaginary "student"'s question and an answer. We assume that you more or less know BASIC, and have a basic idea of how it works. So, let's begin. Q: So. I've heard a lot about C, and it is supposed to be a very popular language. Is this true? A: Well, yes. Many professionals and amateurs use it, mostly because of its speed and efficiency, which surpasses many others. It was brought over from the UNIX world, and there it gained a large base of followers. Q: How come a language can be faster than another? A: The answer is relatively complex.. To begin with, we have to differentiate between "interpreted" and "compiled" languages. BASIC is usually interpreted while C usually compiled. The main difference is the time when the program is translated into machine language, the only language that can be run by the microprocessor. Interpreted languages translate the lines of code individually, every time they are executed. The translation takes a lot of time, because it is a very complicated operation. So, in BASIC, for instance, if you say: FOR I=1 to 2000 PRINT I,SQR(I)*I/2 NEXT I The line "PRINT..." will be translated to the same machine language instructions 2000 times. This wastes a lot of time. Compiled languages, however, only translate each line ONCE, and store the machine language code. Once the compiler translates the whole program, it is saved on disk usually, and can be run with no further intervention from the compiler. Because each program line was already translated, even if a line is executed thousands of times, there will be NO time wasted translating it again and again. Q: Is this the only difference? A: Well, not really. Another place where interpreted languages are slowed down is when they are using variables. In the preceding example, for instance, the variable "I" is looked for over 10 000 times during the duration of the loop. 10 000! And since BASIC interpreters usually have to look through internal lists to find variables, it slows them down a lot. Compiled languages, however, only have to look for a variable ONCE, and whenever that variable is referenced sometime later, the compiler will substitute the previously found location in memory of that variable, and will never have to explicitly search for it again and again. This is the other major speed improvement. Q: Hmm.. Tell me about variables. Are they the same in C as they are in BASIC? A: Well, almost, except in C one has a much larger selection of variable types than in BASIC.. Here is a list of the variable types allowed in BASIC: Integer - -32768 to +32767 - eg. 391 Floating point - -far to +far - eg. -293.372 String - characters - eg. "Hello There" Array - A group of any variable type Arrays are a special type, as you know, in that they can be large groups of any other type - even arrays! Here is a short list of the simple variable types in C, each detailing just how much space one of these variables really takes up, and just what it can represent. Byte 1 byte long range: -128 to 127 (256 different values) Unsigned Byte 1 byte long Just like Byte but the range is 0 to 255 (256 altogether) Integer 2 bytes long range: -32768 to 32767 (65536 different values) Unsigned Integer 2 bytes long range: 0 to 65535 (65536 different values) Longword 4 bytes long range: -2147483650 to 214783649 (2^32 different values) Unsigned Longword 4 bytes long range: 0 to 4294967300 (2^32 different values) Float 4 bytes long range: -far to +far precision is limited.. But the range is quite good Double precision Float 8 bytes long range: -farther to +farther precision is Much better than that of Float. But takes up twice as much space Character 1 byte long contains ASCII characters, like A or c or &.. In C, there is no "string" type. This is because, as far as the compiler is concerned, a string is just an array of characters. As in BASIC, arrays in C can contain a list of anything - even arrays. Another note: In actual C, there is no distinction between the "Byte" and the "Character" because they both fit into exactly 1 byte, and one can perform the exact same arithmetic operations on both. One can even use character constants in expressions involving bytes, or integers. Q: Hmm.. How are different variable types named? A: In C, unlike in BASIC, you don't need to append a special character at the end of the variable names, simply because the compiler already knows what type of variable it is. So, you don't have to say WOMBAT$ for a string, or FOOBAR% for an integer. In C, you only have to define a variable once, and from there, the C compiler will know what you mean. The process of actually telling the compiler about a variable will be detailed in a later tutorial. Q: Well, that sounds good. What does an average C program look like? I mean, the program lines. Similar to BASIC? A: Everything is organized into "functions". A function is a routine that may or may not return a value. Parameters can be passed to it, if you want. Look at this example program. I will explain it as we go along. In most programs, the programmer will put in comments to the reader of the program so they understand more of just how the program works. These comments are enclosed by two symbols - /* to begin a comment, and */ to end one. Nothing between these symbols will be looked at by the compiler. main() /* The main function */ { printf("Hello World\n"); /* Print a message to the user.. */ } Let me explain. "main" is the name of the function which is called when a program is run, that is, the operating system and startup instructions call main() automatically. The body of the function is enclosed in "{" and "}". The symbols could be translated as "Block Begin" and "Block End". As you will see, there are many places where these symbols are used, but they are always used in pairs. Functions usually have many statements between the "{" and the "}" signs, but in this simple example, there is only one. The third line contains one statement - a call to a function named printf(). It is passed as an argument the string "Hello World\n", where "\n" is a special code "newline" which, on the Amiga, is translated into an LF character (ASCII code #10.). The argument to the printf() function is put between a pair of parentheses - "()". This function does not return any values. The statement, which is the call to printf(), is finished with a ";" character. All C statements must end with ";". This is because the compiler does not otherwise know when a statment ends, because it does not care about the end of line. To show this, look at this example, which is quite identical, as far as the C compiler is concerned, to the first example, above. main() { printf("Hello World\n"); } The matter of putting statements on separate lines is convention, which makes reading programs much easier. Q: But, you said there was no variable type equivalent to strings in BASIC. A: Good point. Actually, since strings are very often used, C does support a form of it, namely, enclosing characters in quotes ("). What it really is translated to is an array of characters with a 00 byte at the end, to show the end of the string. The process is quite transparent, though, and you can use strings very similarly to that in BASIC. Q: Hmm.. I think I understand. Tell me more. Not today. Well, folks, I think that's about it for the basic introduction. We covered compiled versus interpreted languages, and the basic variable types used in C. We also know approximately how programs are built.. If this is the first time you read this, you might want to read it again, but this time capturing it in a file and perhaps printing it out later. Any errors that you may have noticed, or any comments and questions should be placed in the new message base - C TUTORIAL. Frank Ch. Eigler -- sysop